package com.jyy.prefabricated.controller;


import com.aliyun.oss.common.utils.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itextpdf.text.BadElementException;
import com.itextpdf.text.Image;
import com.jyy.prefabricated.common.AjaxResult;
import com.jyy.prefabricated.dto.CertificateDto;
import com.jyy.prefabricated.dto.Pageable;
import com.jyy.prefabricated.entity.Certificate;
import com.jyy.prefabricated.entity.SysUser;
import com.jyy.prefabricated.service.CertificateService;
import com.jyy.prefabricated.utils.UserUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;


/**
 * 证书
 * @version 1.0.0
 * @author: zjj
 */
@Slf4j
@RequestMapping("/api")
@RestController
public class CertificateController {

    @Autowired
    private CertificateService certificateService;

    @Autowired
    private UserUtils userUtils;
    /**
     * 新增证书
     * @param certificateDto
     * @return
     */
    @PostMapping("/certificates")
    public AjaxResult insert(@Valid @RequestBody CertificateDto certificateDto){
        log.info("Rest request to insert certificate:{}",certificateDto);
        Integer id = certificateService.insert(certificateDto);
        CertificateDto result = certificateService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 修改证书
     * @param certificateDto
     * @return
     */
    @PutMapping("/certificates")
    public AjaxResult update(@Valid @RequestBody CertificateDto certificateDto){
        log.info("Rest request to update certificate:{}",certificateDto);
        certificateService.update(certificateDto);
        CertificateDto result = certificateService.selectById(certificateDto.getId());
        return AjaxResult.success(result);
    }

    /**
     * 查询证书详情
     * @param id
     * @return
     */
    @GetMapping("/certificates/{id}")
    public AjaxResult selectById(@PathVariable Integer id){
        log.info("Rest request to find certificate by id:{}",id);
        CertificateDto result = certificateService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 删除证书
     * @param id
     * @return
     */
    @DeleteMapping("/certificates/{id}")
    public AjaxResult deleteById(@PathVariable Integer id){
        log.info("Rest request to delete certificate by id:{}",id);
        int row = certificateService.delete(id);
        if(row > 0){
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 分页查询证书
     * @param pageable
     * @return
     */
    @GetMapping("/certificates")
    public AjaxResult selectByParams(Pageable pageable){
        log.info("Rest request to find certificates by params");
        PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<Certificate> certificates = certificateService.selectAll();
        PageInfo<Certificate> result = new PageInfo<>(certificates);
        return AjaxResult.success(result);
    }

    /**
     * 查询证书列表
     * @return
     */
    @GetMapping("/certificates-list")
    public AjaxResult selectAll(){
        log.info("Rest request to find certificates list");
        List<Certificate> result = certificateService.selectAll();
        return AjaxResult.success(result);
    }
}
